import request = require("request");
import { Constant } from '../Core/FMAppAPIConstant'
import { APIRequestType } from "../Core/FMAppAPIConstant";
import { RequestMethod } from '../Core/FMAppAPIConstant';
import { json } from "body-parser";
export class ReqeustManager<T> {
    public async Request(reqeustType: string, requestMethod: RequestMethod, data: any): Promise<T> {
        const promise = new Promise<T>((callback) => {
            let url: string = Constant.FMAppRequestUrl + reqeustType;
            if (requestMethod == RequestMethod.GET) {
                //todo for pending 
            } else {
                //body json
                let requestObj = {

                };
                request.post(url, { body: JSON.stringify(data), headers: this.getHeaders() }, (err, response, body) => {
                    if (err) {
                        console.log("reqeust api :" + reqeustType + ":: error:" + err);
                        callback(null);
                        return;
                    }
                    if (response.statusCode != 200) {
                        console.log("reqeust api:" + reqeustType, "::status code:" + JSON.stringify(response));
                        callback(null);
                        return;
                    }

                    const result = JSON.parse(body) as T;
                    callback(result);
                });
            }

        });
        return promise;
    }
    public getHeaders():any{
        return {
            "Content-type": "application/json; charset=UTF-8",
            "Accept": "application/json; charset=UTF-8",       
        };
    }
}
